package com.easygoods.request;

import com.bestvike.linq.IEnumerable;
import com.bestvike.linq.Linq;
import com.bestvike.stone.core.lang.StringUtils;
import com.bestvike.stone.spring.exception.AssertUtils;
import com.bestvike.stone.spring.rest.IRequest;
import lombok.Data;

import java.util.List;

/**
 * Created by 许崇雷 on 2019-01-24.
 */
@Data
public class OrderTrailRequest implements IRequest {
    private List<OrderCommitRequestSku> skuList;
    private List<String> couponList;

    @Override
    public void assertValid() {
        //商品不为空
        AssertUtils.notEmpty(this.skuList, "skuList");
        Linq.asEnumerable(this.skuList).forEach(OrderCommitRequestSku::assertValid);
        //跨商户下单
        final int merchantCount = Linq.asEnumerable(this.skuList).select(OrderCommitRequestSku::getMerchantId).distinct().count();
        AssertUtils.isTrue(merchantCount == 1, "不能跨商户下单");
        //sku 重复验证
        final int skuCount = Linq.asEnumerable(this.skuList).select(OrderCommitRequestSku::getSkuId).distinct().count();
        AssertUtils.isTrue(skuCount == this.skuList.size(), "sku 不允许重复");
        //代金券验证
        if (this.couponList != null) {
            final IEnumerable<String> coupons = Linq.asEnumerable(this.couponList).distinct();
            AssertUtils.isTrue(coupons.count() == this.couponList.size() && coupons.all(StringUtils::isNotEmpty), "代金券不允许重复并且代金券号不能为空");
        }
    }
}
